/*
 * =====================================================================================
 *
 *       Filename:  c_string.h
 *
 *    Description:  String utils for C. 
 *
 *        Version:  1.0
 *        Created:  10/10/2010 02:35:56 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Justin Lei GAO (leikao), leikao@gmail.com
 *        Company:  
 *
 * =====================================================================================
 */


#ifndef  C_STRING_INC
#define  C_STRING_INC

#include	<string.h>


/*-----------------------------------------------------------------------------
 *  全局参数 
 *-----------------------------------------------------------------------------*/
#define MAX_STRING_LEN 1024 /* 最大字符串长度 */


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  strip_head
 *  Description:  Remove blanks from the heading of a string. 
 *        Input:  
 *       Output:  
 *       Return:  出错返回 -1；否则返回处理后的字符串长度。 
 * =====================================================================================
 */
    int
strip_head ( char* src, char* dst )
{
    int rc = -1; 
    
    if ( NULL == dst ) {
        rc = -1; 
        return rc; 
    }

    int src_len = strlen(src); /* 判断入参的长度 */
    if ( MAX_STRING_LEN < src_len ) {
        rc = -1; 
        return rc; 
    }
    if ( 0 == src_len ) {
        rc = 0; 
        dst[0] = '\0'; 
        return rc; 
    }

    int i = 0; 
    
    while ( i != src_len ) {
        char cur_char = src[i]; 

        if ( ' ' == cur_char || '\t' == cur_char ) {
            ++i; 
            continue; 
        }
        else {
            break; 
        }
    }
    rc = src_len - i + 1; /* 拷贝的字符串长度 */
    memcpy(dst, (char*)&src[i], (src_len - i + 1)); 
    dst[rc] = '\0'; 

    return rc;
}		/* -----  end of function strip_head  ----- */


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  strip_tail
 *  Description:  Remove blanks at the tail of a string. 
 *        Input:  
 *       Output:  
 *       Return:  出错返回 -1；否则返回处理后的字符串长度。 
 * =====================================================================================
 */
   int 
strip_tail ( char* src, char* dst )
{
    int rc = -1; 

    if ( NULL == dst ) {
        rc = -1; 
        return rc; 
    }

    /* 判断入参长度 */
    int src_len = strlen(src); 
    if ( MAX_STRING_LEN < src_len ) {
        rc = -1; 
        return rc; 
    }
    if ( 0 == src_len ) {
        dst[0] = '\0'; 
        rc = 0; 
        return rc; 
    }

    int i = src_len - 1; 
    while ( 0 <= i ) {
        char cur_char = src[i]; 
        if ( ' ' == cur_char || '\t' == cur_char ) {
            --i; 
            continue; 
        }
        else
        {
            break; 
        }
    }
    
    rc = i + 1;                                 /* 拷贝字符串的长度 */
    memcpy(dst, src, rc); 
    dst[rc] = '\0'; 

    return rc;
}		/* -----  end of function strip_tail  ----- */


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  strip
 *  Description:  Remove the blanks from the head and tail of a string. 
 *        Input:  
 *       Output:  
 *       Return:  出错返回 -1；否则返回处理后的字符串长度。 
 * =====================================================================================
 */
    int
strip ( char* src, char* dst )
{
    int rc = -1; 
    char tmp_str[MAX_STRING_LEN + 1] = {0}; 

    rc = strip_head(src, tmp_str);              /* 掐头 */
    if ( -1 == rc ) {
        return rc; 
    }

    rc = strip_tail(tmp_str, dst);              /* 去尾 */
    if ( -1 == rc ) {
        return rc; 
    }

    return rc;
}		/* -----  end of function strip  ----- */


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  strip_all
 *  Description:  Remove all the blanks from a string. 
 *        Input:  
 *       Output:  
 *       Return:  出错返回 -1；否则返回处理后的字符串长度。 
 * =====================================================================================
 */
    int
strip_all ( char* src, char* dst)
{
    int rc = -1; 
    
    if ( NULL == dst ) {
        rc = -1; 
        return rc; 
    }

    char tmp_str[MAX_STRING_LEN + 1] = {0}; 
    rc = strip_head(src, tmp_str);              /* 掐头 */
    if ( -1 == rc ) {
        return rc; 
    }

    int i = 0; 
    int j = -1; 
    char last_char = ' '; 
    int src_len = strlen(tmp_str); 

    while ( i != src_len ) {
        char cur_char = tmp_str[i]; 

        if ( ' ' == cur_char || '\t' == cur_char ) {
            if ( ' ' != last_char && '\t' != last_char ) {
                dst[++j] = ' '; 
            }
        }
        else {
            dst[++j] = cur_char; 
        }
        ++i; 
        last_char = cur_char; 
    }

    if ( ' ' == dst[j] || '\t' == dst[j] ) {    /* 删除最后一个空格，并增加行结束符 */
        dst[j] = '\0'; 
        rc = j; 
    }
    else
    {
        rc = j + 1; 
        dst[rc] = '\0'; 
    }

    return rc;
}		/* -----  end of function strip_all  ----- */


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  get_fields_num
 *  Description:  Get the words' number of a string. 
 *        Input:  
 *       Output:  
 *       Return:  出错返回 -1；否则返回单词数量。 
 * =====================================================================================
 */
    int
get_fields_num ( char* src )
{
    int rc = -1; 
    char dst[MAX_STRING_LEN + 1 ] = {0}; 

    rc = strip(src, dst); 
    if ( -1 == rc ) {
        return rc; 
    }

    int i = 0; 
    rc = 0; 
    int dst_len = strlen(dst); 
    char last_char = ' '; 

    while ( i != dst_len ) {
        char cur_char = dst[i]; 

        if ( ' ' != cur_char && '\t' != cur_char ) {
            if ( ' ' == last_char || '\t' == last_char ) {
                ++rc; 
            }
        }
        ++i; 
        last_char = cur_char; 
    }
    return rc;
}		/* -----  end of function get_fields_num  ----- */


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  get_field_value
 *  Description:  
 *        Input:  
 *       Output:  
 *       Return:  出错返回 -1；否则返回单词长度。 
 * =====================================================================================
 */
    int
get_field_value ( char* src, int idx, char* word )
{
    int rc = -1; 
    char dst[MAX_STRING_LEN + 1 ] = {0}; 

    rc = strip(src, dst); 
    if ( -1 == rc ) {
        return rc; 
    }

    int i = 0; 
    rc = 0; 
    int dst_len = strlen(dst); 
    char last_char = ' '; 
    int start_pos = 0;                          /* 单词开始位置 */
    int end_pos = 0;                            /* 单词结束位置 */

    while ( i != dst_len ) {
        char cur_char = dst[i]; 

        if ( ' ' != cur_char && '\t' != cur_char ) {
            if ( ' ' == last_char || '\t' == last_char ) {
                ++rc; 
                
                if ( idx == rc ) {
                    start_pos = i; 
                    
                    while ( i != dst_len ) {
                        cur_char = dst[i]; 
                        if ( ' ' != cur_char && '\t' != cur_char  ) {
                            ++i; 
                        }
                        else
                        {
                            break; 
                        }
                    }
                    end_pos = i; 
                    break; 
                }
            }
        }
        ++i; 
        last_char = cur_char; 
    }

    rc = end_pos - start_pos; 
    memcpy(word, (char*)&dst[start_pos], rc); 
    word[rc] = 0; 

    return rc;
}		/* -----  end of function get_field_value  ----- */


#endif 
